from rest_framework.compat import unicode_to_repr

from utils.common import getmd5


class BaseHashId(object):
    def __init__(self, id_name=None, hash_field=None):
        self.hash_field = hash_field
        self.id_name = id_name

    def set_context(self, serializer_field):
        content = serializer_field.context.get('request').data.get(self.hash_field)
        setattr(self, self.id_name, getmd5(content))

    def __call__(self):
        return getattr(self, self.id_name)

    def __repr__(self):
        return unicode_to_repr('%s()' % self.__class__.__name__)


